import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class IpSearch {
	
	public static long ip2long(String ip)  {
        return int2long(str2int(ip));
    }
	private static int str2int(String ip){
		String[] ss = ip.split("\\.");
        int a, b, c, d;
        a = Integer.parseInt(ss[0]);
        b = Integer.parseInt(ss[1]);
        c = Integer.parseInt(ss[2]);
        d = Integer.parseInt(ss[3]);
        return (a << 24) | (b << 16) | (c << 8) | d;
    }
	private static long int2long(int i) {
		long l = i & 0x7fffffffL;
		if (i < 0) {
			l |= 0x80000000L;
		}
		return l;
	}

	
	public static void main(String []args){
		String regEx = "(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
		Pattern pattern = Pattern.compile(regEx);// 编译正则表达式
		Scanner in = new Scanner(System.in);
		MonIpDAO ipd = new MonIpDAO();
		String input;
		do{
			System.out.println("请输入要查询的IP地址：");
			input = in.next();
		    Matcher matcher = pattern.matcher(input);
		    
			while(!matcher.matches()){	  
				System.out.println("输入格式有误，请重新输入,按e退出。");
				input = in.next();
				if(input.equals("e")){
					System.out.println("再见~");
					return;
				}
				matcher = pattern.matcher(input);
			}
			IP ip = ipd.ipSearch(input,ip2long(input));
			System.out.println(ip.toString());	
			System.out.println("继续查询请输入\"y\"，输入其余的退出。");
			input = in.next();
		}while(input.equals("y"));
		System.out.println("再见~");
	}
}
